/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Modele.Case;
import Modele.Grille;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.imageio.ImageIO;
import javax.swing.JPanel;


/**
 *
 * @author david
 */
public class DamierPanel extends JPanel implements Observer{
     
    Image image_damier;
    Image image_pion_blanc;
    Image image_pion_noir;
    Image image_dame_blanc;
    Image image_dame_noire;
    Image image_pion_selectionner;
    Grille modele;
       
    
    public DamierPanel(Grille m){
        chargerImage(); 
        this.modele = m;
        
        // Add observers 
        for(int i = 0; i<10; i++)      
            for( int j = 0 ; j < 10 ; j++)
            {                
                 modele.getCase(i, j).addObserver(this);
            }
    }
    
    private void chargerImage(){
        try {
            image_damier = ImageIO.read(new File("damier.png"));
            image_pion_blanc = ImageIO.read(new File("pion_blanc.png"));
            image_pion_noir = ImageIO.read(new File("pion_noir.png"));
            image_pion_selectionner = ImageIO.read(new File("pion_selectionner.png"));
            image_dame_blanc = ImageIO.read(new File("dame_blanc.png"));
            image_dame_noire = ImageIO.read(new File("dame_noir.png"));
        } catch (IOException ex) {
            System.out.print(ex);
        }
    }
    
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image_damier, 0, 0, this.getWidth(), this.getHeight(), this);
        
        // BOUCLE DE RENDU
        //----------------------------------------------------------------------
        for(int i = 0; i<10; i++)      
            for( int j = 0 ; j < 10 ; j++)
            {                
                Case c = modele.getCase(i, j);
                switch(c.getEtat()){
                    case PION_BLANC:
                        dessinerPionBlanc(g, i, j);
                        break;
                    case PION_NOIR:
                        dessinerPionNoir(g, i, j);
                        break;
                    case SELECTIONNER:
                        dessinerPionSelectionner(g,i,j);
                        break;
                    case DAME_BLANC:
                        dessinerDameBlanc(g,i,j);
                        break;
                    case DAME_NOIRE:
                        dessinerDameNoire(g,i,j);
                        break;
                }
            }         
        //----------------------------------------------------------------------
    }
    
    private void dessinerPionBlanc(Graphics g , int case_y , int case_x){
        
        g.drawImage(image_pion_blanc,(this.getWidth()/10)*case_x+1,(this.getHeight()/10)*case_y+1,this.getWidth()/10,this.getHeight()/10, this);
    }
    
    private void dessinerPionNoir(Graphics g , int case_y , int case_x){
        g.drawImage(image_pion_noir,(this.getWidth()/10)*case_x+1,(this.getHeight()/10)*case_y+1,this.getWidth()/10,this.getHeight()/10, this);
    }
    
    private void dessinerPionSelectionner(Graphics g , int case_y , int case_x){
        g.drawImage(image_pion_selectionner,(this.getWidth()/10)*case_x+1,(this.getHeight()/10)*case_y+1,this.getWidth()/10,this.getHeight()/10, this);
    }
    
    private void dessinerDameBlanc(Graphics g , int case_y , int case_x){
        g.drawImage(image_dame_blanc,(this.getWidth()/10)*case_x+1,(this.getHeight()/10)*case_y+1,this.getWidth()/10,this.getHeight()/10, this);
    }
    
    private void dessinerDameNoire(Graphics g , int case_y , int case_x){
        g.drawImage(image_dame_noire,(this.getWidth()/10)*case_x+1,(this.getHeight()/10)*case_y+1,this.getWidth()/10,this.getHeight()/10, this);
    }

    @Override
    public void update(Observable o, Object arg) {
        repaint();
    }
    
    public int[] getCase(int x, int y){
        
        x-= 10; y-=30; // Les positions de la souris prennent en compte les rebords de la fenêtre (il faut donc les enlever)
        
        int[] pos = new int[2];
        pos[1] = x / (this.getWidth() /10) ; 
        pos[0] = (y / (this.getHeight() /10));
        return pos;
    }
}
